﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ArchSoft
{
    public partial class InputFrm : Form
    {
        Thread myth = null;
        bool fl = false;
        bool fl2 = false;
        string CurFol = "";
        ProgressingWin fr = null;
        public InputFrm(string ProjectFolder)
        {
            CurFol = ProjectFolder;
            InitializeComponent();
            foreach (CheckBox bx in this.groupBox2.Controls)
            {
                bx.CheckedChanged += new EventHandler(bx_CheckedChanged);
            }

            foreach (CheckBox bx in this.groupBox1.Controls)
            {
                bx.CheckedChanged += new EventHandler(fbx_CheckedChanged);
            }
        }

        void fbx_CheckedChanged(object sender, EventArgs e)
        {
            if (fl2)
                return;
            fl2 = true;
            foreach (CheckBox bx in this.groupBox1.Controls)
            {
                if (!bx.Equals(sender))
                    if (bx.Checked == true)
                        bx.Checked = false;
            }
            fl2 = false;
        }

        void bx_CheckedChanged(object sender, EventArgs e)
        {
            if (fl)
                return;
            fl = true;
            foreach (CheckBox bx in this.groupBox2.Controls)
            {
                if (!bx.Equals(sender))
                    if (bx.Checked == true)
                        bx.Checked = !((CheckBox)sender).Checked;
            }
            fl = false;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog Fls = new FolderBrowserDialog();
            Fls.Description = "Select Input Folder";
            Fls.ShowNewFolderButton = false;
            if (Fls.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = Fls.SelectedPath;
            }
        }


        //Start Copy!
        private void button2_Click(object sender, EventArgs e)
        {
            string ToF = "";
            if (this.textBox1.Text.Trim() == "")
            {
                //MessageBox.Show("Please browse a folder to start the command.", "Fail Command", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show("Xin vui lòng duyệt qua một thư mục để bắt đầu lệnh .", "Lệnh lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtDes.Text == "")
            {
                //MessageBox.Show("Please fill in description field", "Issued", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show("Vui lòng nhập vào phần mô tả", "Phát hành", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            foreach (CheckBox obx in this.groupBox2.Controls)
            {
                if (obx.Checked)
                {
                    ToF = obx.Text;
                    break;
                }
            }
            if (ToF == "")
            {
                //MessageBox.Show("Please select a type in group Type.", "Unknown Command", MessageBoxButtons.OK, MessageBoxIcon.Information);
                MessageBox.Show("Hãy lựa chọn một loại trong nhóm Loại", "Không biết lệnh", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            ToF = "";
            foreach (CheckBox obx in this.groupBox1.Controls)
            {
                if (obx.Checked)
                {
                    ToF = obx.Text;
                    break;
                }
            }
            if (ToF == "")
            {
                //Hay Chon Loai Folder!
                return;
            }

            myth = new Thread(new ThreadStart(CopyIn));
            myth.IsBackground = true;
            fr = new ProgressingWin(myth);
            fr.MessText = "None";
            fr.Text = "Chấp nhận thông tin vào";
            //Hide();
            this.Opacity = 0;
            //this.ShowInTaskbar = false;
            fr.ShowDialog();
            Close();
        }

        string GetDate()
        {
            DateTime dateTime = DateTime.Now;
            return dateTime.Year + "." + dateTime.Month.ToString("00") + "." + dateTime.Day.ToString("00");
        }

        private string GetUniqueName(string mainName, string StoreDir)
        {
            int order = 0;
            string result = mainName;

            while (Directory.Exists(StoreDir + @"\" + result))
            {
                order++;
                result = mainName + "(" + order + ")";
            }
            return result;
        }

        void CopyIn()
        {
            string Typ = "";
            try
            {
                foreach (CheckBox bx in this.groupBox1.Controls)
                {
                    if (bx.Checked)
                    {
                        if (Typ == "")
                            foreach (CheckBox obx in this.groupBox2.Controls)
                            {
                                if (obx.Checked)
                                {
                                    Typ = obx.Text;
                                    break;
                                }
                            }

                        //string CpHere = CurFol + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INPUT_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString() + Path.DirectorySeparatorChar + Typ;
                        //CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(GetDate(), CpHere)+"_"+txtDes.Text;

                        /*Rut gon lai folder, tao theo dinh dang YYMMDD_DWG_Desc*/
                        string CpHere = CurFol + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INPUT_FOLDER + Path.DirectorySeparatorChar + bx.Tag.ToString();
                        CpHere = CpHere + Path.DirectorySeparatorChar + GetUniqueName(CodeNameUtilities.GetDateYYMMDD(DateTime.Now), CpHere) + "_" + Typ + "_" + txtDes.Text;
                        MoveDirectory(this.textBox1.Text, CpHere, false);
                        //if (MessageBox.Show("Inputing folder [" + CpHere + "] completed. Do you want to open it?", "Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        if (MessageBox.Show("Thông tin vào thư mục [" + CpHere + "] hoàn hành. Bạn có muốn mở thư mục?", "Hoàn thành", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            System.Diagnostics.Process.Start(CpHere);
                    }

                }//Ket thuc Duyet
            }
            catch { }
        }

        void MoveDirectory(string strSourceDir, string strDestDir, bool bDelSource)
        {
            if (Directory.Exists(strSourceDir))
            {
                try
                {
                    CopyDirectory(new DirectoryInfo(strSourceDir), new DirectoryInfo(strDestDir));
                }
                catch (Exception subEx)
                {
                    throw subEx;
                }
            }
        }

        void CopyDirectory(DirectoryInfo diSourceDir, DirectoryInfo diDestDir)
        {
            if (!diDestDir.Exists) diDestDir.Create();
            FileInfo[] fiSrcFiles = diSourceDir.GetFiles();
            foreach (FileInfo fiSrcFile in fiSrcFiles)
            {
                fiSrcFile.CopyTo(Path.Combine(diDestDir.FullName, fiSrcFile.Name));
            }
            DirectoryInfo[] diSrcDirectories = diSourceDir.GetDirectories();
            foreach (DirectoryInfo diSrcDirectory in diSrcDirectories)
            {
                CopyDirectory(diSrcDirectory, new DirectoryInfo(Path.Combine(diDestDir.FullName, diSrcDirectory.Name)));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}